//
//  XPNetworkTools.m
//  SinaWeibo
//
//  Created by 徐攀 on 16/5/13.
//  Copyright © 2016年 徐攀. All rights reserved.
//

#import "XPNetworkTools.h"


typedef void(^SuccessBlock)(NSURLSessionDataTask *task, id response);
typedef void(^FailureBlock)(NSURLSessionDataTask *task, NSError *error);

@interface XPNetworkTools ()

@property (nonatomic, copy) SuccessBlock successResult;
@property (nonatomic, copy) FailureBlock failureResult;

@end

@implementation XPNetworkTools

+ (instancetype)sharedTool {
    static XPNetworkTools *tools;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tools = [[XPNetworkTools alloc] init];
       tools.responseSerializer.acceptableContentTypes = [tools.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
    });
    return tools;
}

- (void)requestWithType:(RequestType)type URLString:(NSString *)URLString parameters:(id)parameters completeBlock:(CompleteBlock)completeBlock {
    // 获取数据成功后回调的block
    self.successResult = ^(NSURLSessionDataTask *task, id response){
        if (completeBlock) completeBlock(response,nil);
    };
    // 获取数据失败后回调的block
    self.failureResult = ^(NSURLSessionDataTask *task, NSError *error){
        if (completeBlock) completeBlock(nil,error);
    };
    // 根据参数type确定请求的方法
    if (type == RequestTypePOST) {
        [self POST:URLString parameters:parameters progress:nil success:self.successResult failure:self.failureResult];
    }else{
        [self GET:URLString parameters:parameters progress:nil success:self.successResult failure:self.failureResult];
    }
    
    
//    if (type == RequestTypePOST) {
//        [self POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            if (completeBlock) {
//                completeBlock(responseObject,nil);
//            }
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            if (completeBlock) {
//                completeBlock(nil,error);
//            }
//        }];
//    }else{
//        [self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            if (completeBlock) {
//                completeBlock(responseObject,nil);
//            }
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            if (completeBlock) {
//                completeBlock(nil,error);
//            }
//        }];
//    }
}
//{
//    uid = 2639838130;
//    expires_in = 157679999;
//    access_token = 2.003UUesCZOWOsB0c6621e0f62Pri7C;
//    remind_in = 157679999;
//}
- (void)requestAccessToken:(NSString *)code finishedBlock:(FinishedBlock)finishedBlock {
    NSDictionary *parameters = @{
                                 @"client_id":kClientId,
                                 @"client_secret":kAppSecret,
                                 @"grant_type":@"authorization_code",
                                 @"code":code,
                                 @"redirect_uri":kRedirectUri
                                 };
    
    [self requestWithType:RequestTypePOST URLString:@"https://api.weibo.com/oauth2/access_token" parameters:parameters completeBlock:^(id response, NSError *error) {
        if (error != nil ) {
            if (finishedBlock) {
                finishedBlock(NO);
            }
            
            NSLog(@"%@",error);
            return;
        }
        
        finishedBlock(YES);
    }];
}




@end
